import http from '@/utils/request';

// 获取当前用户ID
const getUid = () => {
  const currentAccount = uni.getStorageSync('current_account_id');
  return currentAccount || '';
};

// 构建请求参数
const buildParams = (params = {}) => {
  return {
    uid: getUid(),
    version: '2.0.0',
    ...params
  };
};

// 用户相关接口
export const getUserInfo = () => {
  return http.post('/v8/api/user/home', {
    uid: getUid(),
    version: '2.0.4'
  });
};

// 宗门相关接口
export const getUnionInfo = (data) => {
  return http.post('/v8/api/game/room/union/my', buildParams(data));
};

// 账号相关接口
export const login = (data) => {
  return http.post('/api/account/login', data);
};

export const getAccountInfo = () => {
  return http.post('/api/account/info');
};

// 灵珠相关接口
export const getPearlCount = () => {
  return http.post('/api/pearl/count');
};

export const plunderPearl = (data) => {
  return http.post('/api/pearl/plunder', data);
};

// 祈福相关接口
export const getTempleInfo = (data) => {
  return http.post('/v8/api/game/union/temple/index', buildParams(data));
};

export const startPray = (data) => {
  return http.post('/v8/api/game/union/temple/pray/start', buildParams(data));
};

// 域外天魔相关接口
export const getBossInfo = (data) => {
  return http.post('/v8/api/game/world/boss/data', buildParams(data));
};

export const attackBoss = (data) => {
  return http.post('/v8/api/game/world/boss/battle/process', buildParams(data));
};

// 妖塔相关接口
export const getTowerConfig = (data) => {
  return http.post('/v8/api/game/room/union/beast/tower/battle/config', buildParams(data));
};

export const startTowerBattle = (data) => {
  return http.post('/v8/api/game/room/union/beast/tower/battle', buildParams(data));
};

export const attackBeast = (data) => {
  return http.post('/v8/api/game/room/union/beast/tower/attack', buildParams(data));
};

export const getTowerResult = (data) => {
  return http.post('/v8/api/game/room/union/beast/tower/refresh', buildParams(data));
};

// 宝箱相关接口
export const getBoxInfo = (data) => {
  return http.post('/v8/api/game/ruins/box', buildParams(data));
};

export const openBox = (data) => {
  return http.post('/v8/api/game/ruins/box/open', buildParams(data));
};

// 导出所有API
export default {
  user: {
    getUserInfo
  },
  union: {
    getUnionInfo
  },
  account: {
    login,
    getAccountInfo
  },
  pearl: {
    getPearlCount,
    plunderPearl
  },
  temple: {
    getTempleInfo,
    startPray
  },
  boss: {
    getBossInfo,
    attackBoss
  },
  tower: {
    getTowerConfig,
    startTowerBattle,
    attackBeast,
    getTowerResult
  },
  box: {
    getBoxInfo,
    openBox
  }
};
